import axios from "axios"
import router from "./router"
import { Notification } from "element-ui"

// vendors
import "normalize.css"
import "iconfont/iconfont.css"

// axios
const isDev = process.env.NODE_ENV === "development"
Object.assign(axios.defaults, {
  baseURL: isDev ? "http://localhost:5000" : "/api",
  withCredentials: true,
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
})
axios.interceptors.response.use(
  res => {
   return  res.data
    // switch(res.data.code) {
    //   case 1: // 成功
    //     return res.data
    //   case 10200: // 请求参数错误
    //   case 19999: // 内部异常
    //     Notification.error("出错了")
    //     return Promise.reject(new Error(res.data.msg))
    //   default:
    //     Notification.error(res.data.msg)
    //     return Promise.reject(new Error(res.data.msg))
    // }
  },
  err => {
    const res = err.response
    switch(res.status) {
      case 401:
        router.push("/login")
        break
      default:
        Notification.error("未知网络错误")
    }
  },
)
